using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SpellCenter.Core;

namespace SpellCenter.UI.FileChoosing
{
    public partial class FileTreeControl : UserControl
    {
        private readonly FileTreeControlPM _model;
        private FilesInCorpusList _CorpusConfig;

        public FileTreeControl()
        {
            InitializeComponent();
            _tree.FireHandleCreated();
            _model = new FileTreeControlPM(_tree);
            AddIgnorePatternsToModel();
            RegisterEvents();
        }

        private void AddIgnorePatternsToModel()
        {
            _model.AddToIgnoreList(new Regex(@"\.bak$", RegexOptions.IgnoreCase));
            _model.AddToIgnoreList(new Regex(@"^\.svn$"));
            _model.AddToIgnoreList(new Regex(@"~$"));
        }

        public DirectoryInfo Directory
        {
            get { return _model.Directory; }
        }

        public FilesInCorpusList CorpusConfig
        {
            get { return _CorpusConfig; }
            set
            {
                if(value != null)
                    _model.LoadConfigFile(value);
                _CorpusConfig = value;
            }
        }

        #region RegisterEvents and Event Handlers

        private void RegisterEvents()
        {
            Resize += Control_Resize;
            _tree.BeforeExpand += Tree_BeforeExpand;
//            _tree.AfterCheck += Tree_AfterCheck;
        }

        private void Tree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            _model.ExpandNode(e.Node);
        }

//        private void Tree_AfterCheck(object sender, TreeViewEventArgs e)
//        {
//            if(_model == null) return;
//
//            // unregister event for this method, so it doesn't cause infinite recursion
//            _tree.AfterCheck -= Tree_AfterCheck;
//            if (!e.Node.Checked)
//            {
//                _model.UncheckNode(e.Node);
//            }
//            else
//            {
//                _model.CheckNode(e.Node);
//            }
//            _tree.AfterCheck += Tree_AfterCheck;
//        }

        private void Control_Resize(object sender, EventArgs e)
        {
            _tree.Size = Size;
        }

        #endregion

        public IEnumerable<FileInfo> GetIncludedFiles()
        {
            return _model.GetIncludedFiles();
        }

        public IEnumerable<FileInfo> GetExcludedFiles()
        {
            return _model.GetExcludedFiles();
        }

        /// <summary>
        /// Check all nodes that match filetype <code>type</code>
        /// </summary>
        /// <param name="type"></param>
        public void CheckFileType(string type)
        {
            _model.CheckFileType(type);
        }
        /// <summary>
        /// Uncheck all nodes that match filetype <code>type</code>
        /// </summary>
        /// <param name="type"></param>
        public void UncheckFileType(string type)
        {
            _model.UncheckFileType(type);
        }

        public static bool IsChecked(TreeNode node)
        {
            if(node.StateImageIndex == TriStateTreeView.STATE_CHECKED)
                return true;
            else return false;
        }
        public void CheckNode(TreeNode node)
        {
            _tree.CheckNode(node);
        }
        public void UncheckNode(TreeNode node)
        {
            _tree.UncheckNode(node);
        }
    }
}
